package org.com.mars.controller.system;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.com.fw.util.EncryptUtil;
import org.com.fw.util.StringUtil;
import org.com.mars.controller.util.ControllerUtil;
import org.com.mars.pojo.system.SysGroup;
import org.com.mars.pojo.system.SysRole;
import org.com.mars.pojo.system.SysUser;
import org.com.mars.pojo.system.SysUserGroup;
import org.com.mars.pojo.system.SysUserRole;
import org.com.mars.service.system.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping({ "/system/user" })
public class UserController {

	@Resource
	private SysUserService sysUserService;
	
	/**
	 * 根据用户ID查询用户信息
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("user.xht")
	public String user(Model model, HttpServletRequest request, 
			HttpServletResponse response) throws IOException {

		return ControllerUtil.getControllerRetrun(request);
	}

	/**
	 * 根据用户ID查询用户信息
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("getSysUserByUserId.xht")
	public String getSysUserByUserId( 
			HttpServletRequest request, HttpServletResponse response,
			@RequestParam("userId") String userId) 
					throws IOException {

		SysUser user = sysUserService.getSysUserByUserId(userId);

		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", "0");
		json.put("user", user);
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 根据用户ID查询用户信息
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("getSysUserAll")
	public String getSysUserAll(
			HttpServletRequest request, HttpServletResponse response) 
					throws IOException {

		List<SysUser> users = sysUserService.getSysUserAll();

		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", "0");
		json.put("data", users);
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 追加用户信息.
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "addSysUser.xht", method = RequestMethod.POST)
	public String getSysUserByUserId(
			HttpServletRequest request, HttpServletResponse response) 
					throws IOException {
		
		
		String userName = "";
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", "0");
		json.put("userName", userName);
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 更新用户信息.
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateSysUserByUserId")
	public String updateSysUserByUserId(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId)
					throws IOException {
		
		int result = sysUserService.updateSysUserByUserId(userId);

		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", result);
		json.put("message", "message");
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 修改密码.
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @param userPassword
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updateSysUserPwdByUserId")
	public String updateSysUserPwdByUserId(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("userPassword") String userPassword) 
					throws IOException {
		String encrptPassword = EncryptUtil.encryptSha256(userPassword);
		
		int result = sysUserService.updateSysUserPwdByUserId(userId, encrptPassword);

		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", result);
		json.put("message", "message");
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 删除用户信息（物理删除）.
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 */
	@RequestMapping("delSysUserById")
	public String delSysUserById(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId) 
					throws IOException {
		int result = sysUserService.delSysUserByUserId(userId);
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", result);
		json.put("message", "message");
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 删除用户信息（伦理删除：禁用）.
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("delSysUserByUserId2")
	public String delSysUserByUserId2(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId)  
					throws IOException {
		int result = sysUserService.delSysUserByUserId2(userId);
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", result);
		json.put("message", "message");
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 查询组所有信息 (可用only).
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("getSysUserGroupByUserId")
	public String getSysUserGroupByUserId(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId)  
					throws IOException {
		List<Map<String, Object>> groups = new ArrayList<Map<String, Object>>();
		List<SysGroup> sysGroups = sysUserService.getSysGroupAll2();
		if (sysGroups != null && !sysGroups.isEmpty()) {
			List<SysUserGroup> sysUserGroups = sysUserService.getSysUserGroupByUserId(userId);
			for (SysGroup sysGroup : sysGroups) {
				Map<String, Object> group = new HashMap<String, Object>();
				group.put("groupId", sysGroup.getGroupId());
				group.put("groupName", sysGroup.getGroupName());
				SysUserGroup sysUserGroup = new SysUserGroup();
				sysUserGroup.setGroupId(sysGroup.getGroupId());
				if (sysUserGroups != null && sysUserGroups.contains(sysUserGroup)) {
					group.put("selected", true);
				} else {
					group.put("selected", false);
				}
				groups.add(group);
			}
		}
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", "1");
		json.put("groups", groups);
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 更新用户组有信息.
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updUserGroupByUserId")
	public String updUserGroupByUserId(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("userGroup") String userGroup)  
					throws IOException {
		
		// 删除寄存的数据
		sysUserService.delSysUserGroupByUserId(userId);
		
		// 追加用户到组
		if (StringUtil.isNotEmpty(userGroup)) {
			String[] groups = userGroup.split(",");
			for (String g : groups) {
				if (StringUtil.isNumber(g)) {
					SysUserGroup sysUserGroup = new SysUserGroup();
					sysUserGroup.setUserId(userId);
					sysUserGroup.setGroupId(g);
					sysUserService.addSysUserGroupByUserId(sysUserGroup);
				}
			}
		}
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", "1");
		ControllerUtil.sendJsonToResult(response, json);
		
		return null;
	}
	
	/**
	 * 查询角色所有信息 (可用only).
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("getSysUserRoleByUserId")
	public String getSysUserRoleByUserId(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId)  
					throws IOException {
		List<Map<String, Object>> roles = new ArrayList<Map<String, Object>>();
		List<SysRole> sysRoles = sysUserService.getSysRoleAll2();
		if (sysRoles != null && !sysRoles.isEmpty()) {
			List<SysUserRole> sysUserRoles = sysUserService.getSysUserRoleByUserId(userId);
			for (SysRole sysRole : sysRoles) {
				Map<String, Object> role = new HashMap<String, Object>();
				role.put("roleId", sysRole.getRoleId());
				role.put("roleName", sysRole.getRoleName());
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setRoleId(sysRole.getRoleId());
				if (sysUserRoles != null && sysUserRoles.contains(sysUserRole)) {
					role.put("selected", true);
				} else {
					role.put("selected", false);
				}
				roles.add(role);
			}
		}
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", "1");
		json.put("roles", roles);
		ControllerUtil.sendJsonToResult(response, json);

		return null;
	}
	
	/**
	 * 更新用户角色信息.
	 * 
	 * @param model
	 * @param request
	 * @param response
	 * @param userId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("updUserRoleByUserId")
	public String updUserRoleByUserId(HttpServletRequest request, 
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("userRole") String userRole)  
					throws IOException {
		
		// 删除寄存的数据
		sysUserService.delSysUserRoleByUserId(userId);
		
		// 追加用户到角色
		if (StringUtil.isNotEmpty(userRole)) {
			String[] roles = userRole.split(",");
			for (String r : roles) {
				if (StringUtil.isNumber(r)) {
					SysUserRole sysUserRole = new SysUserRole();
					sysUserRole.setUserId(userId);
					sysUserRole.setRoleId(r);
					sysUserService.addSysUserRoleByUserId(sysUserRole);
				}
			}
		}
		
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("rt", "1");
		ControllerUtil.sendJsonToResult(response, json);
		
		return null;
	}
}
